﻿namespace OpenPms.Services;

public partial class OrderService : OrderServiceBase
{
    public OrderService(IServiceProvider serviceProvider) : base(serviceProvider)
    {
    }

    public async Task SendWXMsgAsync(int hotelId, EWXMsgType msgType, string msg)
    {
        var url = configuration.GetValue<string>(Consts.SectionApplicationUrl);
        var item = await db.WXMsgs.FindAsync(hotelId, msgType);
        if (!string.IsNullOrEmpty(item?.UserIds))
            await wx.SendMDMessageAsync(item.CorpId, item.UserIds, $"{msgType}\n{msg}");
    }

    private async Task SendCancelWXMsgAsync(TReservation reservation)
    {
        var item = await db.WXMsgs.FindAsync(reservation.HotelId, EWXMsgType.Ota取消消息);
        if (!string.IsNullOrEmpty(item?.UserIds))
            await wx.SendMDMessageAsync(item.CorpId, item.UserIds, $"Ota取消订单\n{reservation.RoomTypeName}:{reservation.Quantity}间\n[详情]({configuration.GetValue<string>(Consts.SectionApplicationUrl)}/...)");
    }
    private async Task SendFailCreateReservationWXMsgAsync(TReservation reservation,int canOrderQuantity)
    {
        var item = await db.WXMsgs.FindAsync(reservation.HotelId, EWXMsgType.Ota新订失败消息);
        if (!string.IsNullOrEmpty(item?.UserIds))
            await wx.SendMDMessageAsync(item.CorpId, item.UserIds, $"{reservation.SourceName}新订失败\n{reservation.RoomTypeName}:{reservation.Quantity}间\n可定数量：{canOrderQuantity}\n[详情]({configuration.GetValue<string>(Consts.SectionApplicationUrl)}/...)");
    }
    private async Task SendSuccessCreateReservationWXMsgAsync(TReservation reservation)
    {
        var item = await db.WXMsgs.FindAsync(reservation.HotelId, EWXMsgType.Ota新订成功消息);
        if (!string.IsNullOrEmpty(item?.UserIds))
            await wx.SendMDMessageAsync(item.CorpId, item.UserIds, $"{reservation.SourceName}新订单\n{reservation.RoomTypeName}:{reservation.Quantity}间\n[详情]({configuration.GetValue<string>(Consts.SectionApplicationUrl)}/...)");
    }

    private async Task SendCheckInReservationWXMsgAsync(TOrder order)
    {
        var item = await db.WXMsgs.FindAsync(order.HotelId, EWXMsgType.入住消息);
        if (!string.IsNullOrEmpty(item?.UserIds))
            await wx.SendMDMessageAsync(item.CorpId, item.UserIds, $"入住\n{order.RoomTypeName}:{order.RoomNo}\n[详情]({configuration.GetValue<string>(Consts.SectionApplicationUrl)}/...)");
    }

}

public class OrderServiceBase
{
    private readonly IServiceProvider serviceProvider;
    public OrderServiceBase(IServiceProvider serviceProvider)
    {
        this.serviceProvider = serviceProvider;
    }
    private AppDbContext? _db;
    private ILogger<OrderService>? _logger;
    private IOss? _oss;
    private WXWork3rd? _wx;
    private IConfiguration? _config;
    protected IConfiguration configuration
    {
        get
        {
            if (_config is null)
                _config = serviceProvider.GetRequiredService<IConfiguration>();
            return _config;
        }
    }
    protected AppDbContext db
    {
        get
        {
            if (_db is null)
                _db = serviceProvider.GetRequiredService<AppDbContext>();
            return _db;
        }
    }
    protected ILogger<OrderService> logger
    {
        get
        {
            if (_logger is null)
                _logger = serviceProvider.GetRequiredService<ILogger<OrderService>>();
            return _logger;
        }
    }
    protected IOss oss
    {
        get
        {
            if (_oss is null)
                _oss = serviceProvider.GetRequiredService<IOss>();
            return _oss;
        }
    }
    protected WXWork3rd wx
    {
        get
        {
            if (_wx is null)
                _wx = serviceProvider.GetRequiredService<WXWork3rd>();
            return _wx;
        }
    }
}